/** %^SOF $id[MAIN] */

#include "main.h"

void Initilize(void)
{

	uart_init(UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU));
	wdt_enable(WDTO_1S);
	sei();

	uart_clr();

	uart_puts_P(_APP_TITLE_);
	//printf("\n\r**[VERSION: %s.%s - BUILD: %s @ %s]",_APP_VER_,_APP_REV_,__DATE__,__TIME__);
	//uart_puts_P("\b\r** Welcome, System has been initialized and ready for use...\n\r\n\rBegin:\n\r");

	SETBIT(DDRD,PD7);
	CLEARBIT(PORTD,PD7);
}

void ReadSettings(void)
{
	if(eeprom_read_byte((uint8_t*)ASWITCH_ADDR))
	{
	SETBIT(PORTD,PD7);
	} else {
		CLEARBIT(PORTD,PD7);
	}

}

int main(void)
{
    unsigned int c,InpBuffCur=0; //,sCmdCount=1;
    char InpBuff[255];
    char sCmd[25], sParam[25], sStrt; //,sCmdOk=0, sInputType=0, sFailFlag=0;
    volatile uint8_t  *iAddr, x;

    Initilize();
    ReadSettings();
    while(1)
    {
    for(;;)
    {
    	wdt_reset();
        c = uart_getc();
        if ( c & UART_NO_DATA )
        {
        	wdt_reset();
        }
        else
        {

			/** --------- @s Error Check ---------- **/
            if ( c & UART_FRAME_ERROR )
            {
                uart_puts_P("Frame Error! ");
				break;
            }
            if ( c & UART_OVERRUN_ERROR )
            {
                uart_puts_P("Overrun Error!");
				break;
            }
            if ( c & UART_BUFFER_OVERFLOW )
            {
                uart_puts_P("Buffer overflow error!");
				break;
            }
			/** --------- Error Check @e ---------- **/



            if(c!=(unsigned char)0x0D)
            {
            	// Add input to input buffer
            	InpBuff[InpBuffCur]=c;
            	InpBuffCur++;
            }
            else
            {
            	while (uart_getc() != UART_NO_DATA) // Empty UART RX ringbuffer if extra chars where sent
            	{
            		nop();
            	}
            	InpBuff[InpBuffCur]=0x20; //Add a whitespace to ensure old crap dont matter..
            	//sInputType=0;
            	//sFailFlag=0;
            	//sCmdOk=0;

            	sscanf(InpBuff,"%c %s ( %s ) ;",&sStrt,sCmd,sParam);


            	if(sCmd[strlen(sCmd)-1] == (char)';') //If ends with ';' then there was no space, so lets deleted it.
				{
					sCmd[strlen(sCmd)-1] = 0x00;
				}


            	//printf("\n\r[%03u] ",sCmdCount);
            	/*
            	switch (sStrt) {
					case (char)CMD_CHAR:
							sInputType = CMD_CODE;
							uart_puts_P("CMD");
							break;
					case (char)HELP_CHAR:
							uart_puts_P("HELP");
							sInputType = HELP_CODE;
							break;
					case (char)INFO_CHAR:
							uart_puts_P("INFO");
							sInputType = INFO_CODE;
							break;
					//case (char)RST_CHAR:
					//		while(1) { _reset(); }
					//		break;
					default:
							uart_puts_P("!ERROR!");
							sFailFlag = 1;
							break;
            	}
				*/


            	/*
            	printf(" > \"%s\" [%s]",sCmd,((sInputType)? "OK" : "UF"));
				*/

            	////////
            	/// BEGIN COMMAND PARSING

            	//_delay_ms(20);
            	if(sStrt==(char)CMD_CHAR) //////////////////////////////// CMD_CODE ////////////////////
            	{
					if(strcmp(sCmd,"toggle")==0)
					{
						if(CHECKBIT(PORTD,PD7))
						{
							CLEARBIT(PORTD,PD7);
						} else {
							SETBIT(PORTD,PD7);
						}
						eeprom_write_byte((uint8_t*)ASWITCH_ADDR,CHECKBIT(PORTD,PD7));
						printf("\n\rASwitch %s",((CHECKBIT(PORTD,PD7))? "ON" : "OFF" ));
						//sCmdOk=1;
					}

					if(strcmp(sCmd,"reset")==0)
					{
						_reset();
						//sCmdOk=1;
					}

            	} else if (sStrt==(char)HELP_CHAR) {
            		//sParam
            		if(strcmp(sCmd,"param")==0)
					{
            			//c=uart_getc();
            			printf("\n\rParam: %s [%u]",sParam, strlen(sParam));
						//sCmdOk=1;
					}

            	} else if (sStrt==(char)INFO_CHAR) {

					if(strcmp(sCmd,"get")==0)
					{
						iAddr   = (unsigned char *)((unsigned int)u2h(atoi(sParam)));
						x = *iAddr;
						printf("\n\rGET 0x%02X = 0x%02X",(u2h(atoi(sParam))),x);
					}

            	} else {
            		printf("\n\rSyntax Error!");
            	}

            	//_delay_ms(20);
            	/*if(sCmdOk) {
            		printf("\n\rCommand Completed...");
            	} else {
            		printf("\n\rCommand not found...");
            	}*/

            	memset(InpBuff,0x00,InpBuffCur); //CLEAR INPBUFF?
            	memset(sCmd,0x00,strlen(sCmd));
            	memset(sParam,0x00,strlen(sParam));

            	InpBuffCur=0;
            	//sCmdCount++;
            	//uart_puts_P("\n\r");

            	while(uart_getc() != UART_NO_DATA)
            	{
            		nop();
            	}
            }
		}
    }
	}
    _reset();
    return 0;
}
